<?php

// application/forms/Tblcompany.php
class Application_Form_Tblcompany extends OMNZend_Form {

    private $idCompany;
    private $logger;

    public function setIdCompany($idCompany) {
        $this->idCompany = $idCompany;
    }

    public function getIdCompany() {
        return $this->idCompany;
    }

    public function init() {

        //$this->view->headTitle('BBViande - Adresse');
        $this->logger = Zend_Registry::get('logger');
        $this->logger->info("Hello from Application_Form_Tblcompany");

        // Set the method for the display form to POST
        $this->setMethod('post');

        // Add the Siret element
        $siret = new Zend_Form_Element_Text('siret');
        $siret->setLabel('Siret');
        // Add the Nom element
        $nom = new Zend_Form_Element_Text('nom');
        $nom->setLabel('Nom')
                ->setRequired(true)
                ->addValidator('NotEmpty');

        // Add the Description element
        $description = new Zend_Form_Element_Text('description');
        $description->setLabel('Description')
                ->setRequired(true)
                ->addValidator('NotEmpty');

        $adresse01 = new Zend_Form_Element_Text('adresse01');
        $adresse01->setLabel('Adresse');
        $adresse01->setAttrib('size', '42');

        $codepostal01 = new Zend_Form_Element_Text('codepostal01');
        $codepostal01->setLabel('Code postal');

        $pays01 = new Zend_Form_Element_Text('pays01');
        $pays01->setLabel('Pays');

        $email01 = new Zend_Form_Element_Text('email01');
        $email01->setLabel('Email');

        $tel01 = new Zend_Form_Element_Text('tel01');
        $tel01->setLabel('Telephone');

        $fax01 = new Zend_Form_Element_Text('fax01');
        $fax01->setLabel('Fax');

        $adresse02 = new Zend_Form_Element_Text('adresse02');
        $adresse02->setLabel('Adresse 2');
        $adresse02->setAttrib('size', '42');

        $codepostal02 = new Zend_Form_Element_Text('codepostal02');
        $codepostal02->setLabel('codepostal02');

        $pays02 = new Zend_Form_Element_Text('pays02');
        $pays02->setLabel('Pays 2');

        $email02 = new Zend_Form_Element_Text('email02');
        $email02->setLabel('Email 2');

        $tel02 = new Zend_Form_Element_Text('tel02');
        $tel02->setLabel('Telephone 2');

        $fax02 = new Zend_Form_Element_Text('fax02');
        $fax02->setLabel('Fax 2');

        // Add the Active element
        $active_element = new Zend_Form_Element_Select('active');
        $active_element->setLabel('Active')
                ->setMultiOptions(array('Y' => 'Y', 'N' => 'N'))
        ;

        // Add the Level element
        $level_element = new Zend_Form_Element_Select('level');
        $level_element->setLabel('Level')
                ->setMultiOptions(array('0' => '0', '1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5', '6' => '6', '7' => '7', '8' => '8', '9' => '9'))
        ;
        $level_element->setValue(array('1' => '1'));

        
        // Add the Fournisseur element
        $fournisseur_element = new Zend_Form_Element_Select('fournisseur');
        $fournisseur_element->setLabel('Fournisseur')
                ->setMultiOptions(array('Y' => 'Y', 'N' => 'N'))
        ;
        $fournisseur_element->setValue(array('N' => 'N'));

        //$this->addElement(new Application_Form_Element_ClientSelect());
        $client_element = new Zend_Form_Element_Select('client');
        $client_element->setLabel('Client')
                ->setMultiOptions(array('Y' => 'Y', 'N' => 'N'))
        ;

        // Add the submit button
        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel('Add Company');

        $this->addElements(array($siret, $nom, $description,
                                 $adresse01, $codepostal01, $pays01, 
                                 $email01, $tel01, $fax01,$active_element, $level_element,
                                 $fournisseur_element, $client_element, 
                                 $adresse02, $codepostal02, $pays02, $email02, $tel02, $fax02, $submit,));
        
        $this->clearDecorators();

        $this->addDecorator('FormElements')
                ->addDecorator('HtmlTag', array('tag' => '<ul>', 'class' => 'form'))
                ->addDecorator('Form');

        $this->setElementDecorators($this->_defaultDecorator);

        $this->getElement('siret')->setDecorators($this->_floatLeftDecorator);
        $this->getElement('nom')->setDecorators($this->_floatLeftDecorator);
        $this->getElement('description')->setDecorators($this->_defaultDecorator);
        
        $this->getElement('adresse01')->setDecorators($this->_floatLeftDecorator);
        $this->getElement('codepostal01')->setDecorators($this->_floatRightDecorator);
        $this->getElement('pays01')->setDecorators($this->_floatLeftDecorator);
        $this->getElement('email01')->setDecorators($this->_floatRightDecorator);
        $this->getElement('tel01')->setDecorators($this->_floatLeftDecorator);
        $this->getElement('fax01')->setDecorators($this->_floatRightDecorator);      
        
                
        $this->getElement('active')->setDecorators($this->_floatLeftDecorator);
        $this->getElement('level')->setDecorators($this->_floatRightDecorator);
        $this->getElement('fournisseur')->setDecorators($this->_floatLeftDecorator);
        $this->getElement('client')->setDecorators($this->_floatRightDecorator);
        
        $this->getElement('adresse02')->setDecorators($this->_floatLeftDecorator);
        $this->getElement('codepostal02')->setDecorators($this->_floatRightDecorator);
        $this->getElement('pays02')->setDecorators($this->_floatLeftDecorator);
        $this->getElement('email02')->setDecorators($this->_floatRightDecorator);
        $this->getElement('tel02')->setDecorators($this->_floatLeftDecorator);
        $this->getElement('fax02')->setDecorators($this->_floatRightDecorator);


        $this->getElement('submit')->setDecorators($this->_submitDecorator);

        $idCompany = $this->getIdCompany();
        if (isset($idCompany) && $idCompany != "") {
            $company = new Application_Model_DbTable_Tblcompany( );

            $company = $company->fetchRow(array("id = ?" => $idCompany));
            if ($company != null) {
                $company = $company->toArray();
                $this->populate($company);
                //$this->logger->info(Zend_Debug::dump($company, $label = null, $echo = true));

                $submit->setLabel('Update Company');
            } else {
                throw new Zend_Exception("form_company_no_id");
            }
        }
    }

}
